package org.jsping.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * @author ping 请求工具类
 */
public class AuthUtil {
	public static final String WXAPPID = "wxbacd61197eb886ea";
	public static final String WXAPPSECRET = "588250595a4e954f1c4e9ecb4e2d3fd0";

	public static final String QQAPPID = "101453713";
	public static final String QQAPPSECRET = "3ebc5ab92274f3b3af5c1ba96b865994";

	/**
	 * @param url
	 *            请求URL
	 * @return 返回json数据
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
		JSONObject json = null;
		// DefaultHttpClient client = new DefaultHttpClient();
		HttpClient client = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			String result = EntityUtils.toString(entity, "UTF-8");
			json = JSONObject.parseObject(result);
		}
		httpGet.releaseConnection();
		return json;
	}

	/**
	 * @param url
	 *            请求url
	 * @return 返回String数据
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String doGetString(String url) throws ClientProtocolException, IOException {
		String info = null;
		HttpClient client = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			info = EntityUtils.toString(entity, "UTF-8");
		}
		httpGet.releaseConnection();
		return info;
	}

	/**
	 * 获取音乐的请求方法
	 * 
	 * @param url
	 *            请求url
	 * @return 返回数据
	 */
	public static String doGetMusic(String url) throws IOException {
		String strResult = null;
		HttpClient client = HttpClientBuilder.create().build();
		// 发送get请求
		HttpGet request = new HttpGet(url);
		HttpResponse response = client.execute(request);
		/** 请求发送成功，并得到响应 **/
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			/** 读取服务器返回过来的json字符串数据 **/
			strResult = EntityUtils.toString(response.getEntity());
		}
		request.releaseConnection();
		return strResult;
	}
	/**
	 * 获取歌词
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static String doGetMusicLrc(String url) throws IOException {
		String strResult = null;
		HttpClient client = HttpClientBuilder.create().build();
		// 发送get请求
		HttpGet request = new HttpGet(url);
		request.setHeader("referer","https://y.qq.com/portal/player.html");
		HttpResponse response = client.execute(request);
		/** 请求发送成功，并得到响应 **/
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			/** 读取服务器返回过来的json字符串数据 **/
			strResult = EntityUtils.toString(response.getEntity());
		}
		request.releaseConnection();
		return strResult;
	}
	
}
